/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Shopping;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Administrator
 */
public class ShopcartJTable{
    private final JTable jTable;
    private static HashMap<String, Integer> hm = new HashMap<String, Integer>();
    
    public ShopcartJTable(JTable jTable) {
        this.jTable = jTable;
    }
    
    public boolean Addgoods(String goodsname, Integer goodscount) {
	hm.put(goodsname, goodscount);
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();
        List<Goods> goodslist = getAllGoods();
        for(int j=0;j<goodslist.size();j++)
            if(goodsname.equals(goodslist.get(j).getName())){
                String[] str={String.valueOf(goodslist.get(j).getId()),goodsname,String.valueOf(goodslist.get(j).getPrice()),String.valueOf(goodscount)};
                model.addRow(str);
            }
        return true;
    }

    public boolean upGoods(String goodsname, Integer newNumber) {
        hm.put(goodsname, newNumber);
        displayAll();
        return true;
    }

    public ArrayList<Goods> getAllGoods() {
        ArrayList<Goods> alist=new ArrayList<>();
        alist.add(new Goods(1,"Java",65.3));
        alist.add(new Goods(2,"Math",37.2));
        alist.add(new Goods(3,"短裙",80.0));
        alist.add(new Goods(4,"夹克",46.8));
        alist.add(new Goods(5,"牛奶",65.3));
        return alist;
    }
    
    public void displayAll() {
        List<Map.Entry<String, Integer>> hmlist = new ArrayList<>(hm.entrySet());
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();
        if (hm.isEmpty())
                System.out.println("无任何商品");
        else {
                List<Goods> goodslist = getAllGoods();
                for (int i = 0; i < hm.size(); i++) {
                    for(int j=0;j<goodslist.size();j++)
                        if(hmlist.get(i).equals(goodslist.get(j).getName())){
                            String[] str={String.valueOf(goodslist.get(j).getId()),goodslist.get(j).getName(),String.valueOf(goodslist.get(j).getPrice()),String.valueOf(hmlist.get(i).getValue())};
                            model.addRow(str);
                        }
                }
        }
    }
    
    public boolean Delgoods(int index) {
        List<Map.Entry<String, Integer>> arrayList = new ArrayList<>(hm.entrySet());
        int i=0;
        for (Map.Entry<String, Integer> entry : arrayList) {
            if(i++==index)
                entry=null;
        }
	DefaultTableModel model = (DefaultTableModel)jTable.getModel();
        model.removeRow(index);
        return true;
    }

    public boolean Cleargoods() {
        hm.clear();
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();
        model.setRowCount(0);
        return true;
    }

    public double Sum(){
        List<Map.Entry<String, Integer>> hmlist = new ArrayList<>(hm.entrySet());
        double sum=0;
        if (!hm.isEmpty()){
            List<Goods> goodslist = getAllGoods();
            for(Map.Entry<String, Integer> e:hmlist){
                for(int j=0;j<goodslist.size();j++)
                    if(e.getKey().equals(goodslist.get(j).getName())){
                        sum=sum+goodslist.get(j).getPrice()*e.getValue();
                    }
            }
        }
        return sum;
    }
}
